
package manutençãoempregados.UTILS;

import classesDados.Emprestimo;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.TreeMap;

public class CadastrarEmprestimoDesignadoDAO {
    
    private static TreeMap<String,Emprestimo> designadoTree = new TreeMap<String, Emprestimo>();
    
    public static boolean verificaExistenciaEmprestimo(String code){
        if(designadoTree.containsKey(code)){
               System.out.println(code);
               return true;
           }
           else
               return false;
    }
    
    public static void cadastrarEmprestimo(Emprestimo empr){
       System.out.println("entrou cadempr");
       String code = empr.getEmpregado().getCpf();
       designadoTree.put(code, empr);
       applyEncoder();
       System.out.println("entrou cadempr2");
    }
    
    public static Emprestimo getEmprestimo(String code){
       return designadoTree.get(code);
   }
   
    public static boolean recuperarCadastros(){

           XMLDecoder decoder = null;

           try {
                decoder = new XMLDecoder(new FileInputStream("emprestimos.xml"));
                designadoTree = (TreeMap<String,Emprestimo>) decoder.readObject();     
                decoder.close();
                return true;
            } catch (FileNotFoundException ex) {
                System.out.println(ex.getMessage());
                return false;
            }
       }
    
    private static void applyEncoder(){
       
        System.out.println("entrou cadempr3");
       XMLEncoder encoder = null;
       
       try{
           encoder = new XMLEncoder(new FileOutputStream("empregados.xml"));
           encoder.writeObject(designadoTree);
           System.out.println("entrou cadempr4");
           encoder.close();
       }
       catch(FileNotFoundException  ex){
             System.out.println(ex.getMessage());
       }
   }
}
